package com.hnas.api.util;

import java.math.BigDecimal;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

public class CommonUtil {
	
	public final static ObjectMapper mapper = new ObjectMapper();
	
	 /**   
     * 获取泛型的Collection Type  
     * @param collectionClass 泛型的Collection   
     * @param elementClasses 元素类   
     * @return JavaType Java类型   
     * @since 1.0   
     */   
	public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
		return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
	}
	
	/** 
	* * 两个Double数相减 * 
	*  
	* @param v1 * 
	* @param v2 * 
	* @return Double 
	*/
	public static Double subDouble(Double v1, Double v2){
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.subtract(b2).doubleValue();
    }
	
	 /**
     * 提供精确的加法运算。
     * @param v1 被加数
     * @param v2 加数
     * @return 两个参数的和
     */
    public static Double addDouble(Double v1,Double v2){
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.add(b2).doubleValue();
    }
    
    public static String getDateForma(String tranDate, String tranTime){
    	String showDateForma = "";
    	if(StringUtil.isNotEmpty(tranDate) && 8 == tranDate.length() 
    			&& StringUtil.isNotEmpty(tranTime) && 6 == tranTime.length()){
    		String year = tranDate.substring(0, 4)+"年";
    		String month = tranDate.substring(4, 6)+"月";
    		String day = tranDate.substring(6, 8)+"日";
    		String hour = tranTime.substring(0, 2) + ":";
    		String minute = tranTime.substring(2, 4) + ":";
    		String second = tranTime.substring(4, 6);
    		showDateForma = year + month + day + " " + hour + minute + second;
    	}else{
    		System.out.println("时间格式错误");
    	}
		return showDateForma;
	}
    
    /** 
     * 去除字符串中所包含的空格（包括:空格(全角，半角)、制表符、换页符等） 
     * @param s 
     * @return 
     */  
    public static String removeAllBlank(String s){  
        String result = "";  
        if(null!=s && !"".equals(s)){  
            result = s.replaceAll("[　*| *| *|//s*]*", "");  
        }  
        return result;  
    }
      
    /** 
     * 去除字符串中头部和尾部所包含的空格（包括:空格(全角，半角)、制表符、换页符等） 
     * @param s 
     * @return 
     */  
    public static String trim(String s){  
        String result = "";  
        if(null!=s && !"".equals(s)){  
            result = s.replaceAll("^[　*| *| *|//s*]*", "").replaceAll("[　*| *| *|//s*]*$", "");  
        }  
        return result;  
    }
    
    /** 
     * 去除字符串中GBK格式的特殊空格
     * @param s 
     * @return 
     */  
    public static String trimSpecial(String str){  
        String result = "";  
        if(StringUtil.isNotEmpty(str)){  
            result = str.replace(String.valueOf((char)160)," ").trim();
        }  
        return result; 
    }
    
    public static String getMrchTerminal(String pasmId) {
    	String temp = pasmId.substring(pasmId.length()-4, pasmId.length());
    	String terminal = "460000000" + Integer.toHexString(Integer.parseInt(temp));
    	return terminal;
    }
    
}
